using System;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
using E4RPDevExpress10;
using TakeAway.DataSet;

namespace TakeAway.Controls
{
    public partial class ChangedXtraUserControl : XtraUserControl, IGridView
    {
        private Controller _controller;
        private PedidoDataSet _dataSet;
        
        public Controller Controller
        {
            set
            {
                _controller = value;
                SetDataSource();
                _controller.RequestSaved += delegate { SetDataSource(); };
                _controller.RequestsRefreshed += delegate { SetDataSource(); };
                SetupLocalTypeData();
            }
        }

        public ChangedXtraUserControl()
        {
            InitializeComponent();
            gridView.DoubleClick+= GridViewDoubleClick;
            beginDateEdit.DateTime = DateTime.Now;
            beginDateEdit.Properties.AllowNullInput = DefaultBoolean.False;
            endDateEdit.DateTime = DateTime.Now;
            endDateEdit.Properties.AllowNullInput = DefaultBoolean.False;
        }

        private void SetDataSource()
        {
            gridView.BeginUpdate();
            _dataSet = _controller.GetAllRequests(beginDateEdit.DateTime, endDateEdit.DateTime);
            gridControl.DataSource = _dataSet;
            gridControl.DataMember = _dataSet.Pedido.TableName;            
            gridView.EndUpdate();
        }
        
        private void SetupLocalTypeData()
        {
            var dataSet = new TipoLocalDataSet();
            dataSet.Merge(_controller.GetLocalType());
            localTypeRepositoryItemLookUpEdit.DataSource = dataSet.TipoLocal;
            localTypeRepositoryItemLookUpEdit.DisplayMember = dataSet.TipoLocal.NomeColumn.ToString();
            localTypeRepositoryItemLookUpEdit.ValueMember = dataSet.TipoLocal.IdColumn.ToString();
            localTypeRepositoryItemLookUpEdit.Columns.Clear();
            localTypeRepositoryItemLookUpEdit.Columns.AddRange(new[]
                                                                   {
                                                                       new LookUpColumnInfo("Id", "Id", 70, FormatType.None, "", false, HorzAlignment.Near),
                                                                       new LookUpColumnInfo("Nome", "Nome", 70, FormatType.None, "", true, HorzAlignment.Near)
                                                                   });
        }

        private void GridViewDoubleClick(object sender, EventArgs e)
        {
            if (!_controller.ExistsOpenedRequest(this, "Deseja mesmo efectuar abrir pedido?"))
                return;
            var request = (PedidoDataSet.PedidoRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (request == null)
                return;
            _controller.ShowRequest(request.Id);
        }


        public void RefreshData()
        {
            SetDataSource();
        }

        public GridView GridView()
        {
            return gridView;
        }

        public string ListName
        {
            get { return "TakeAway.Controls.ChangedXtraUserControl"; }
        }

        public PedidoDataSet.PedidoRow GetSelectedRequest()
        {
            if(gridView.SelectedRowsCount == 0)
                return null;
            return (PedidoDataSet.PedidoRow)gridView.GetDataRow(gridView.GetSelectedRows()[0]);
        }
    }
}
